function fig = fm_libfig(varargin)
% FM_LIBFIG create GUI for Limit-Induced Bifurcation analysis
%
% HDL = FM_LIBFIG()
%
%see also FM_LIB
%
%Author:    Federico Milano
%Date:      11-Nov-2002
%Version:   1.0.0
%
%E-mail:    Federico.Milano@uclm.es
%Web-site:  http://www.uclm.es/area/gsee/Web/Federico
%
% Copyright (C) 2002-2013 Federico Milano

global Settings LIB Theme Fig Path

if nargin
  switch varargin{1}

   case 'display'

    if ~LIB.init
      fm_limit
      if ~LIB.init, return, end
    end

    if Settings.matlab && Settings.hostver >= 7.14,
      fm_stat(cellstr(char(['lambda = ', num2str(LIB.lambda)], ...
                           char(fm_strjoin('dlambda/dP_',LIB.bus, ...
                                           ' = ', num2str(LIB.dldp))))));
    else
      fm_stat(cellstr(strvcat(['lambda = ', num2str(LIB.lambda)], ...
                              strvcat(fm_strjoin('dlambda/dP_',LIB.bus, ...
                                                 ' = ', num2str(LIB.dldp))))));
    end

   case 'slack'

    hdl = findobj(Fig.lib,'Tag','Checkbox');
    LIB.slack = ~LIB.slack;
    set(hdl,'Value',LIB.slack)
    if LIB.slack
      set(gcbo,'Checked','on')
    else
      set(gcbo,'Checked','off')
    end

   case 'check'

    LIB.slack = get(gcbo,'Value');
    hdl = findobj(Fig.lib,'Tag','MenuSlack');
    if LIB.slack
      set(hdl,'Checked','on')
    else
      set(hdl,'Checked','off')
    end

  end
  return
end

if Fig.lib, figure(Fig.lib), return, end
%if ~Settings.init,
%  fm_disp('Solve power flow before running LIB computations.')
%  return
%end
types = {'Vmax','Vmin','Qmax','Qmin'};
onoff = {'off','on'};

N = 8;
D = (N+3)*0.039+0.0244;
d1 = 1/(2*N+3);
d2 = 1.2*d1;

h0 = figure('Color',Theme.color01, ...
  'Units', 'normalized', ...
  'ColorMap', [], ...
  'CreateFcn','Fig.lib = gcf;', ...
  'DeleteFcn','Fig.lib = 0;', ...
  'FileName','fm_libfig', ...
  'MenuBar','none', ...
  'Name','LIB Settings', ...
  'NumberTitle','off', ...
  'PaperPosition',[18 180 576 432], ...
  'PaperType','A4', ...
  'PaperUnits','points', ...
  'Position',sizefig(0.3906,D), ...
  'RendererMode','manual', ...
  'Tag','Settings', ...
  'ToolBar','none');

if Settings.hostver >= 5
  % Menu File
  h1 = uimenu('Parent',h0, ...
    'Label','File', ...
    'Tag','MenuFile');
  h2 = uimenu('Parent',h1, ...
    'Callback','fm_limit', ...
    'Label','Run', ...
    'Tag','OTV', ...
    'Accelerator','z');
  h2 = uimenu('Parent',h1, ...
    'Callback','close(gcf)', ...
    'Label','Exit', ...
    'Tag','NetSett', ...
    'Accelerator','x', ...
    'Separator','on');

  % Menu Settings
  h1 = uimenu('Parent',h0, ...
    'Label','Settings', ...
    'Tag','MenuView');
  h2 = uimenu('Parent',h1, ...
    'Callback','fm_libfig slack', ...
    'Label','Distributed Slack Bus', ...
    'Tag','MenuSlack', ...
    'Checked',onoff{LIB.slack+1}, ...
    'Accelerator','s');

  % Menu View
  h1 = uimenu('Parent',h0, ...
    'Label','View', ...
    'Tag','MenuView');
  h2 = uimenu('Parent',h1, ...
    'Callback','fm_libfig display', ...
    'Label','Display results', ...
    'Tag','OTV', ...
    'Accelerator','d');
  h2 = uimenu('Parent',h1, ...
    'Callback','fm_tviewer', ...
    'Label','Select text viewer', ...
    'Tag','NetSett', ...
    'Accelerator','t', ...
    'Separator','on');
end

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'ForegroundColor',Theme.color03, ...
  'Position',[0.038   d1   0.92/2    (2*N+1)*d1], ...
  'Style','frame', ...
  'Tag','Frame1');

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_libfig check', ...
  'Position',[0.078  2*d1   0.356    d2], ...
  'String','Distributed Slack Bus', ...
  'Style','checkbox', ...
  'Tag','Checkbox', ...
  'Value',LIB.slack);

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color04, ...
  'CreateFcn','set(gcbo,''String'',Bus.names)', ...
  'Callback','LIB.selbus = get(gcbo,''Value''); LIB.selbus = LIB.selbus(end); set(gcbo,''Value'',LIB.selbus)', ...
  'ForegroundColor',Theme.color05, ...
  'ListboxTop', LIB.selbus, ...
  'Max', 100, ...
  'Position',[0.078    7*d1   0.356   9*d1], ...
  'String',cell(0,1), ...
  'Style','listbox', ...
  'Tag','Listbox1', ...
  'Value',LIB.selbus);

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'HitTest','off', ...
  'HorizontalAlignment','left', ...
  'Position',[0.078   16.25*d1    0.3   0.8*d2], ...
  'String','Select Bus:', ...
  'Style','text', ...
  'Tag','StaticText1');

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color04, ...
  'Callback','LIB.type = get(gcbo,''Value'');', ...
  'ForegroundColor',Theme.color05, ...
  'Position',[0.078    4*d1   0.356   d2], ...
  'String',types, ...
  'Style','popupmenu', ...
  'Tag','PopupMenu1', ...
  'Value',LIB.type);

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'HitTest','off', ...
  'HorizontalAlignment','left', ...
  'ListboxTop',0, ...
  'Position',[0.078   5.5*d1  0.294  0.8*d2], ...
  'String','Limit Criterion:', ...
  'Style','text', ...
  'Tag','StaticText12');

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color03, ...
  'Callback','fm_limit', ...
  'FontWeight','bold', ...
  'ForegroundColor',Theme.color09, ...
  'Position',[0.558     7*d1    0.356   1.5*d2], ...
  'String','Run', ...
  'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback','close(gcf);', ...
  'Position',[0.558     1*d1   0.356  1.5*d2], ...
  'String','Close', ...
  'Tag','Pushbutton2');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_libfig display', ...
  'Position',[0.558     4*d1   0.356  1.5*d2], ...
  'String','Display Results', ...
  'Tag','Pushbutton3');

h1 = axes('Parent',h0, ...
  'Box','on', ...
  'CameraUpVector',[0 1 0], ...
  'CameraUpVectorMode','manual', ...
  'Color',Theme.color04, ...
  'ColorOrder',Settings.color, ...
  'Layer','top', ...
  'Position',[0.558 12*d1 0.356 5*d2], ...
  'Tag','Axes1', ...
  'XColor',Theme.color05, ...
  'XLim',[0.5 149.5], ...
  'XLimMode','manual', ...
  'XTickMode','manual', ...
  'YColor',Theme.color05, ...
  'YDir','reverse', ...
  'YLim',[0.5 104.5], ...
  'YLimMode','manual', ...
  'YTickMode','manual', ...
  'ZColor',Theme.color05);
h2 = image('Parent',h1, ...
  'CData',imread([Path.images,'logo_lib.jpg'],'jpg'), ...
  'Tag','Axes1Image1', ...
  'XData',[1 150], ...
  'YData',[1 105]);

if nargout > 0, fig = h0; end